package com.bw.controller;

import com.bw.domain.Material;
import com.bw.domain.MaterialClient;
import com.bw.domain.MaterialOrder;
import com.bw.service.MaterialClientService;
import com.bw.service.MaterialOrderService;
import com.bw.service.MaterialService;
import com.bw.upload.UploadService;
import com.bw.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("material")
public class OrderController {

    @Autowired
    MaterialOrderService materialOrderService;

    @Autowired
    MaterialClientService materialClientService;

    @Autowired
    MaterialService materialService;

    @Autowired
    UploadService uploadService;

    @RequestMapping("getclient")
    public Result getclient()
    {
        List<MaterialClient> list = materialClientService.list();
        return Result.success(list);
    }

    @RequestMapping("getmaterial")
    public Result getmaterial()
    {
        List<Material> list = materialService.list();
        return Result.success(list);
    }

    @RequestMapping("add")
    public Result add(@RequestBody MaterialOrder materialOrder)
    {
        boolean save = materialOrderService.save(materialOrder);
        return save?Result.success():Result.fail("添加失败");
    }

    @RequestMapping("list")
    public Result list(@RequestParam(defaultValue = "1")Integer pageNum)
    {
        PageInfo<MaterialOrder> list = materialOrderService.getlist(pageNum);
        return Result.success(list);
    }

    @RequestMapping("uploadimg")
    public Result uploadimg(MultipartFile file) throws IOException {
        String s = uploadService.uploadFile(file);
        return Result.success(s);
    }


    @RequestMapping("delOne")
    public Result delOne(String id)
    {
       boolean b= materialOrderService.delOne(id);
       return b?Result.success():Result.fail("删除失败");
    }


}
